#include "stdafx.h"

/***** Serendipity Booksellers -- mainmenu.cpp *****/

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include "mainmenu.h"
#include "bookdata.h"
#include "cashier.h"
using namespace std;

// Entire book collection
BookCollection books;

// Initialize the static member variable of the Menu
// So that the Menu has a pointer to the collection of books
BookCollection* Menu::books;

void getMenuChoice(int lower, int upper, int& choice);

int _tmain(int argc, _TCHAR* argv[])
{
	Menu::initialize(&books);

	int choice = 0;			// Choice holds user's menu choice

	do
	{
		// Display company name and screen title
		cout << "\n\n\n		Serendipity Booksellers, Mr. HO\n";
		cout << "			Main Menu\n\n";

		// Display menu items
		cout << "	1. Cashier Module\n";
		cout << "	2. Inventory Database Module\n";
		cout << "	3. Report Module\n";
		cout << "	4. Exit\n\n";

		// Display prompt and get user's choice
		cout << "	Enter Your Choice: ";
		getMenuChoice(1, 4, choice);

		// Process user's choice
		switch (choice)
		{
			case 1:	Menu::cashier();
					break;
			case 2:	Menu::invMenu();
					break;
			case 3:	Menu::reports();
					break;
		}
	} while (choice != 4);
	return 0;
}

/*************************************************************************************
 *                               invMenu                                             *
 *	Called by main()                                                                 *
 *	Allows the user to select an inventory operation.                                *
 *************************************************************************************/
void Menu::invMenu()	// Receives array of BookData objects
{
	int choice = 0;		// Stores user's menu choice

	do
	{
		// Display company name and screen title
		cout << "\n\n\n		Serendipity Booksellers, Mr. HO\n";
		cout << "			Inventory Database\n\n\n";

		// Display menu items
		cout << "	1. Look Up a Book\n";
		cout << "	2. Add a Book\n";
		cout << "	3. Edit a Book's Record\n";
		cout << "	4. Delete a Book\n";
		cout << "	5. Return to the Main Menu\n\n";

		// Display prompt and get user's choice
		cout << "	Enter Your Choice: ";
		// Validate user's entry
		getMenuChoice(1, 5, choice);

		switch (choice)
		{
			case 1:	books->lookUpBook();
					break;
			case 2:	books->addBook();
					break;
			case 3:	books->editBook();
					break;
			case 4: books->deleteBook();
					break;
		}
	} while (choice != 5);
}

void Menu::reports()
{
	int choice = 0;

	do
	{
		//Display company name and screen title
		cout << "\n\n\n				Serendipity Booksellers, Mr HO\n";
		cout << "					Reports\n\n\n";

		//Display information items
		cout << "	1. Inventory Listing\n";
		cout << "	2. Inventory Wholesale Value\n";
		cout << "	3. Inventory Retail Value\n";
		cout << "	4. Listing by Quantity\n";
		cout << "	5. Listing by Cost\n";
		cout << "	6. Listing by Age\n";
		cout << "	7. Return to the Main Menu\n\n";

		//Display prompt and get user's choice
		cout << "	Enter Your Choice: ";
		getMenuChoice(1,7,choice);

		//Process user's choice
		switch (choice)
		{
		case 1:	books->repListing();
				break;
		case 2:	books->repWholesale();
				break;
		case 3:	books->repRetail();
				break;
		case 4:	books->repQty();
				break;
		case 5:	books->repCost();
				break;
		case 6:	books->repAge();
				break;
		}
	} while (choice != 7);
}

/*************************************************************************************
 *                               getMenuChoice                                       *
 *	This function validates a menu input as being in                                 *
 *	a specified range.                                                               *
 *************************************************************************************/
void getMenuChoice(int lower, int upper, int &choice)
{
	char ch;
	int isNumber;
	cin >> ch;

	while(true)
	{
		isNumber = isdigit(ch);
		if (isNumber)
		{
			cin.putback(ch);
			cin >> choice;
			if (choice <= upper && choice >= lower)
				return;
		}
		cout << "\n	Please enter a number in the range " << lower << " - " << upper << ". ";
		cin >> ch;
	}
}

void Menu::cashier() {

  Cashier cashier;
  cashier.build();
}